/*********************************************************************************
Organization 					: 				Georgia Institute of Technology
  
Institute 						:				Cognitive Computing Group(CCL)
 
Authors							: 				Santiago Ontanon, Kinshuk Mishra
 												Neha Sugandh 
 												
Class							:				NumberOfUnitsOfTypeCondition
 
Function						: 				Provides a class for holding 
												the condition for number of certain 
												units
****************************************************************************/
package planninglanguage.conditions;

import org.jdom.Element;

import util.XMLWriter;
import base.WargusGameState;
import base.WargusPlayer;
import base.WargusStateImporter;
import base.WargusUnit;


public class NumberOfUnitsOfTypeCondition extends Condition {

	String m_unitType;
	int m_minimumNumber;
	int m_playerID;
	
	public NumberOfUnitsOfTypeCondition(String a_unitType,int a_playerID,int a_minimumNumber)
	{
		m_unitType = a_unitType;
		m_playerID = a_playerID;
		m_minimumNumber = a_minimumNumber;
	}
	public String toString(int tabs)
	{
		String n = super.toString(tabs);
		n = n+ "Player Id is:" + m_playerID;
		n = n + "Unit Type is" + m_unitType;
		n = n + "Minimum Number is" + m_minimumNumber;
		return n; 
	
	}
	public String getUnitType()
	{
		return m_unitType;
	}
	
	public boolean compare(NumberOfUnitsOfTypeCondition c)
	{
		if(m_unitType.equals(c.m_unitType)&& m_playerID==c.m_playerID)
			return true;
		else
			return false;
	}
	public boolean test(WargusGameState a_gameState)
	{		
//		System.out.println("NumberOfUnitsOfTypeCondition: test(player = " + m_playerID + " number = " + m_minimumNumber + " unit_type = " + m_unitType + ")");

		WargusPlayer p = a_gameState.getPlayer(m_playerID);
		
		if (p!=null) {
			int num=0;
			for(WargusUnit u:p.getUnits()) {
				if (WargusStateImporter.unitTypeToString(u.getType()).equals(m_unitType)) num++;			
			}
			
			if (num>=m_minimumNumber) return true;
								 else return false;
			
		}
		return false;	
	}
	
	public void writeXML(XMLWriter w) {
		w.tag("condition type=\""+this.getClass().getSimpleName()+"\"");
		{
			w.tag("unit-type", m_unitType);
			w.tag("player-id", m_playerID);
			w.tag("min-number", m_minimumNumber);
		}
		w.tag("/condition");
	}
	
	protected static Condition loadConditionFromXML(Element e) {
		return new NumberOfUnitsOfTypeCondition(e.getChildText("unit-type"),
				Integer.parseInt(e.getChildText("player-id")),
				Integer.parseInt(e.getChildText("min-number")));
	}
}
